﻿using EnetaMvc.ApplicationServices;
using EnetaMvc.ApplicationServices.Dto;
using EnetaMvc.Infrastructure;
using EnetaMvc.Web.Attributes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EnetaMvc.Web.Controllers
{
    public class SlaMonitoringLogsController : Controller
    {
        private readonly IContentService _contentService;
        private readonly IFileService _fileService;

        public SlaMonitoringLogsController(IContentService contentService, IFileService fileService)
        {
            _contentService = contentService;
            _fileService = fileService;
        }

        //[EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Index(int page = 1)
        {
            if (page < 1) page = 1;
            PagedResult<SlaMonitoringLogDto> slaLogs = _contentService.ListSlaMonitoringLogs(page, 10);
            return View(slaLogs);
        }

        public ActionResult ExportToExcel()
        {
            var temp = _contentService.ListSlaMonitoringLogs(1, 1000);
            var slaLogsDtos = temp.Results.ToList();

            //Response.Clear();
            //Response.ContentType = "application/vnd.ms-excel";
            //Response.AddHeader("Content-Disposition", "attachment; filename=sla_logs.xlsx");
            string filepath = Server.MapPath("~/Content/slalogs.xlsx");
            _fileService.ExportSlaLogsToExcel(slaLogsDtos, filepath);
            Stream outputStream = new FileStream(filepath, FileMode.Open);

            return new FileStreamResult(outputStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }
    }
}
